Skip to content

[lexical] Bug Fix: Shortcuts in a decorator's input act on the input - #9428

Open
Vedthakar wants to merge 4 commits into
meta:mainfrom
Vedthakar:fix/3761-decorator-input-shortcuts
Open

Vedthakar wants to merge 4 commits into
meta:mainfrom
Vedthakar:fix/3761-decorator-input-shortcuts

Conversation

@Vedthakar

@Vedthakar Vedthakar commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Description

This solves issue #3761: some shortcuts do nothing in an input inside a decorator node, for example Ctrl+Delete / Ctrl+Backspace in the playground's poll inputs, or Cmd+A on macOS.

Current behavior. The editor's keydown listener on the root also gets keydowns from a decorator's own <input>. Its shortcut table runs them as editor shortcuts. The prevent(...) shortcuts and select all call event.preventDefault() before dispatching their command:

  • delete word (Ctrl/Alt+Backspace/Delete) and, on Apple, delete line and Ctrl+H/D/K
  • format (Mod+B/I/U)
  • undo / redo
  • select all (Mod+A)
  • Ctrl+O on Apple

So the browser never deletes the word or selects the input's text, and the command acts on the editor's selection instead. beforeinput and input already skip a decorator's captured input (#8740, via isInputEventTargetingCapturedSelection), but keydown didn't.

What this changes.

  • Those shortcuts return without calling preventDefault() or dispatching when the keydown comes from a text input embedded in a node's DOM: a <textarea>, a foreign contentEditable, or an <input> of a type the user types into. This builds on $isEventFromEmbeddedInput, which [lexical-plain-text][lexical-rich-text] Bug Fix: Leave a copy, cut or paste in a decorator's input to the input #9288 added for copy, cut and paste, and excludes the <input> types that take no text (button, checkbox, color, file, image, radio, range, reset, submit).
  • Other controls in a decorator, such as a <button> or a checkbox, keep the editor's shortcuts: the browser has no action of its own for them there.
  • Copy and cut are already handled on main since [lexical-plain-text][lexical-rich-text] Bug Fix: Leave a copy, cut or paste in a decorator's input to the input #9288, so this PR does not touch them.
  • Shortcuts that only dispatch (arrows, Enter, Space, Backspace, Delete, Escape, Tab, Home/End) are unchanged. Their handlers already decide for themselves, and plugins may listen for them from decorators.
  • In a decorator's text input, undo/redo now act on the input (not the editor's history), and format shortcuts and Ctrl+O are no longer cancelled, the same as in any <input> on the page.
  • Nothing changes for keydowns in the editor's own content.

Closes #3761

Test plan

New browser test packages/lexical/src/__tests__/browser/Issue3761DecoratorInputShortcuts.test.ts, with a block decorator holding an <input>:

  • For Word+Backspace, Word+Delete (Ctrl, or Alt on Apple), Mod+A, Mod+B and Mod+Z (and Cmd+Backspace on Apple), a keydown in the input is not defaultPrevented and doesn't dispatch the command.
  • Mod+Z on a <button> or a checkbox inside the decorator is still defaultPrevented and dispatches UNDO_COMMAND.
  • Control for each one: the same keydown in the editor content is still defaultPrevented and dispatches its command once.
  • "a real Ctrl/Alt+Backspace deletes a word in the input": with trusted key presses, hello world becomes hello , and the editor is unchanged.
  • "a real select all selects only the input": with trusted key presses, the input's text is selected and replaced by typing, and the editor is unchanged.

Before

On current main with the test file applied and LexicalEvents.ts from main (Chromium, macOS):

$ pnpm exec vitest --project browser --no-watch --reporter=verbose packages/lexical/src/__tests__/browser/Issue3761DecoratorInputShortcuts.test.ts
 × Shortcuts in a decorator input (#3761) > Word+Backspace in the input is left to the input
 ✓ Shortcuts in a decorator input (#3761) > Word+Backspace in the editor still runs the command (control)
 × Shortcuts in a decorator input (#3761) > Word+Delete in the input is left to the input
 ✓ Shortcuts in a decorator input (#3761) > Word+Delete in the editor still runs the command (control)
 × Shortcuts in a decorator input (#3761) > Mod+A in the input is left to the input
 ✓ Shortcuts in a decorator input (#3761) > Mod+A in the editor still runs the command (control)
 × Shortcuts in a decorator input (#3761) > Mod+B in the input is left to the input
 ✓ Shortcuts in a decorator input (#3761) > Mod+B in the editor still runs the command (control)
 × Shortcuts in a decorator input (#3761) > Mod+Z in the input is left to the input
 ✓ Shortcuts in a decorator input (#3761) > Mod+Z in the editor still runs the command (control)
 × Shortcuts in a decorator input (#3761) > Cmd+Backspace in the input is left to the input
 ✓ Shortcuts in a decorator input (#3761) > Cmd+Backspace in the editor still runs the command (control)
 ✓ Shortcuts in a decorator input (#3761) > Mod+Z on a button in the decorator still runs the command
 ✓ Shortcuts in a decorator input (#3761) > Mod+Z on a checkbox in the decorator still runs the command
 × Shortcuts in a decorator input (#3761) > a real Ctrl/Alt+Backspace deletes a word in the input
 × Shortcuts in a decorator input (#3761) > a real select all selects only the input
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 8 ⎯⎯⎯⎯⎯⎯⎯
 Test Files  1 failed (1)
      Tests  8 failed | 8 passed (16)

After

On this branch (Chromium, macOS):

$ pnpm exec vitest --project browser --no-watch --reporter=verbose packages/lexical/src/__tests__/browser/Issue3761DecoratorInputShortcuts.test.ts
 ✓ Shortcuts in a decorator input (#3761) > Word+Backspace in the input is left to the input
 ✓ Shortcuts in a decorator input (#3761) > Word+Backspace in the editor still runs the command (control)
 ✓ Shortcuts in a decorator input (#3761) > Word+Delete in the input is left to the input
 ✓ Shortcuts in a decorator input (#3761) > Word+Delete in the editor still runs the command (control)
 ✓ Shortcuts in a decorator input (#3761) > Mod+A in the input is left to the input
 ✓ Shortcuts in a decorator input (#3761) > Mod+A in the editor still runs the command (control)
 ✓ Shortcuts in a decorator input (#3761) > Mod+B in the input is left to the input
 ✓ Shortcuts in a decorator input (#3761) > Mod+B in the editor still runs the command (control)
 ✓ Shortcuts in a decorator input (#3761) > Mod+Z in the input is left to the input
 ✓ Shortcuts in a decorator input (#3761) > Mod+Z in the editor still runs the command (control)
 ✓ Shortcuts in a decorator input (#3761) > Cmd+Backspace in the input is left to the input
 ✓ Shortcuts in a decorator input (#3761) > Cmd+Backspace in the editor still runs the command (control)
 ✓ Shortcuts in a decorator input (#3761) > Mod+Z on a button in the decorator still runs the command
 ✓ Shortcuts in a decorator input (#3761) > Mod+Z on a checkbox in the decorator still runs the command
 ✓ Shortcuts in a decorator input (#3761) > a real Ctrl/Alt+Backspace deletes a word in the input
 ✓ Shortcuts in a decorator input (#3761) > a real select all selects only the input
 Test Files  1 passed (1)
      Tests  16 passed (16)

tsc, tsc-test, eslint and prettier on the changed files are clean. Firefox and WebKit were not run locally.


Author: Ved Thakar (@Vedthakar)

🤖 Generated with Claude Code

@vercel

vercel Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Nov 17, 2026 12:20am UTC
lexical-playground Ready Ready Preview Nov 17, 2026 12:20am UTC

Request Review

@pkg-pr-new

pkg-pr-new Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9428

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9428

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9428

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9428

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9428

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9428

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9428

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9428

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9428

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9428

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9428

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9428

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9428

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9428

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9428

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9428

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9428

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9428

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9428

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9428

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9428

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9428

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9428

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9428

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9428

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9428

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9428

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9428

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9428

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9428

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9428

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9428

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9428

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9428

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9428

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9428

commit: 822b190

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Oct 27, 2026

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling #3761! The fix works for the reported case: in the playground poll, Ctrl+Backspace in an option input now deletes a word instead of being swallowed by the editor, and the new browser test cleanly separates the old and new behavior. One issue needs addressing before this can merge.

Undo, redo and select all stop working when a non-text control inside a decorator has focus (packages/lexical/src/LexicalEvents.ts:2109, also :2191 and :2228)

isEventTargetingCapturedSelection returns true for any element inside a decorator, because the reconciler marks every decorator's DOM with setDOMUnmanaged(dom, {captureSelection: true}). So the new early return also fires when focus is on a <button> or a checkbox, where the browser has no default action for Mod+Z, Mod+Shift+Z, Ctrl+Y or Mod+A, and those keys now do nothing. To reproduce in the playground with Chrome: insert a poll, click Add Option, then press Ctrl+Z. On main the added option is undone; with this PR nothing happens. The description scopes the change to a decorator's input, and AGENTS.md's Backwards Compatibility section asks that existing behavior be kept intact otherwise.

Suggested fix: skip only for text-entry targets (a <textarea>, a text-type <input>, or a contentEditable that Lexical doesn't own), similar to how PreventSelectAllExtension limits its skip to INPUT/TEXTAREA targets (packages/lexical-extension/src/PreventSelectAllExtension.ts). A test case with a <button> inside the decorator, asserting that Mod+Z still dispatches UNDO_COMMAND, would lock this in.

Optional, non-blocking:

  • The Mod+C/Mod+X cases only check that the keydown isn't cancelled. With real key presses, Ctrl+X in the input of a node-selected decorator still removes the decorator, through the native cut event and the rich-text cut handler. Since the description leaves the clipboard events to #9288, consider naming those two tests after what they assert.
  • AGENTS.md (Commits and Pull Requests) asks the Test plan's After section to paste the real command output rather than summarize it.

Comment thread packages/lexical/src/LexicalEvents.ts Resolved
Vedthakar and others added 2 commits November 16, 2026 18:36
Keydown shortcuts that preventDefault (word/line delete, select all,
format, undo/redo, Ctrl+O) also ran when an input embedded in a node's
DOM had the focus, so eg. Ctrl+Backspace or Cmd+A did nothing in it.
Skip them when the keydown comes from such an input, as copy and cut
already do. Other controls in a decorator, such as a button, keep the
editor's shortcuts.

Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Vedthakar

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Fixed in the latest push, which also resolves the conflict with main:

Added a changeset. The browser test file passes in Chromium (15 tests).

— Ved Thakar

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking up #3761, and for limiting the skip so a <button> inside a decorator keeps the editor's shortcuts. Two things need fixing before this can merge:

  1. Non-text inputs still lose the editor's undo, redo and select all (packages/lexical/src/LexicalEvents.ts:2132, and the same check at :2216 and :2253). $isEventFromEmbeddedInput is true for any focused <input>, whatever its type, so a checkbox, radio, range or color input inside a decorator now swallows Mod+Z, Mod+Shift+Z, Ctrl+Y and Mod+A without doing anything. To reproduce in the playground: insert a poll, vote by clicking an option's checkbox, then press Ctrl+Z. On main the vote is undone; with this change nothing happens. The test's own rule ("a button has no action of its own for it") applies to these inputs too. Restricting the skip to text-entry targets (a <textarea>, a foreign contentEditable, or an <input> whose type is not button, checkbox, color, file, image, radio, range, reset or submit) keeps the fix, and a checkbox case in the browser test would cover it. If you add a helper for this, keep any lookup table inside the function rather than at module scope (see "Tree-shaking annotations" in AGENTS.md).

  2. The new test fails the TypeScript check (packages/lexical/src/__tests__/browser/Issue3761DecoratorInputShortcuts.test.ts:139). getFirstDescendant no longer takes a type argument on main, so tsc -p ./tsconfig.test.json (part of pnpm run ci-check) reports TS2558 and TS2339 on that line, and the core-tests / integrity job is red. Narrowing works instead:

    const text = $getRoot().getFirstDescendant();
    assert($isTextNode(text));
    text.select(6, 6);

    (import $isTextNode and drop the now-unused TextNode type import).

Optional, non-blocking:

  • The changeset could carry a Verify: paragraph, since the fix changes editing behaviour (undo in a decorator's input now acts on the input). CONTRIBUTING.md ("Changesets") and .changeset/README.md ask for one on such a fix, for example: Verify: keyboard shortcuts (Mod+Z, Mod+A, Ctrl/Alt+Backspace, Mod+B) pressed in an input or textarea inside a decorator; the editor's undo and formatting no longer run for them.
  • The Test plan still describes an earlier version of the branch (Mod+C/Mod+X cases, a helper rename, 16/18 tests, a clean tsc). AGENTS.md ("Commits and Pull Requests") asks for the real Before and After output, so it would help to re-run the test and paste the output for the current file.

Automated review (maintainer link)

Comment thread packages/lexical/src/LexicalEvents.ts Resolved
Comment thread .changeset/decorator-input-shortcuts.md Resolved
getFirstDescendant no longer takes a type argument on main.

Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
$isEventFromEmbeddedInput is true for any <input>, so a checkbox, radio
or range input in a decorator swallowed undo, redo and select all
without doing anything with them. Skip the editor's shortcut only for an
input that takes text.

Add a checkbox case to the browser test and a Verify paragraph to the
changeset.

Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Vedthakar

Copy link
Copy Markdown
Contributor Author

Thanks, both fixed in the latest push:

  1. Non-text inputs: the skip now applies only to a text-entry target (a <textarea>, a foreign contentEditable, or an <input> whose type is not button, checkbox, color, file, image, radio, range, reset or submit), in all three branches. The check lives inside a small helper, with no module-scope table. Added a checkbox case to the browser test; it fails on the previous push.
  2. TypeScript check: fixed, tsc -p ./tsconfig.test.json is clean.

Also added the Verify: paragraph to the changeset, and replaced the Test plan with the real Before and After output of the current test file.

— Ved Thakar

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working through #3761. The core of the fix works: in the playground, Ctrl+Backspace in a poll option now deletes the word in the input instead of sending DELETE_WORD_COMMAND to the editor, and the new browser tests fail without the LexicalEvents.ts change and pass with it. One issue needs fixing before this can land.

Ctrl+Z in a decorator input can now undo the editor's text natively in Chromium (packages/lexical/src/LexicalEvents.ts, the new early return at line 2153 that the undo/redo shortcuts go through)

Chromium keeps one undo stack per page. When the focused decorator input has no edit of its own on top of that stack, Ctrl+Z makes Chromium undo the user's last typing in the editor and fire beforeinput (historyUndo) at the editor root. onBeforeInput skips that event because the focused element is a decorator input ($isInputEventTargetingCapturedSelection, the active-element fallback at lines 1218-1223), so nothing calls preventDefault(), and the historyUndo handling in $handleBeforeInput (lines 1394-1416) never runs. That branch exists, per its #6714 comment, so the browser's native history never rewrites the editor's DOM behind its back; with this change that rewrite becomes reachable from the keyboard.

Steps in the playground (Chromium):

  1. Type a line in the editor, then insert a poll.
  2. Click at the end of the first paragraph and type and more.
  3. Click into the empty "Option 2" input and press Ctrl+Z.

On this branch and more disappears with no UNDO_COMMAND (the tree view logs an INPUT_COMMAND), Redo stays disabled, focus moves out of the poll into the editor, and pressing Ctrl+Z again brings the text back. Firefox keeps undo per control and is not affected.

Suggested direction: in onBeforeInput, decide historyUndo/historyRedo by their composed target. When it is the editor root or Lexical-managed content rather than the focused control, prevent the default (and either dispatch UNDO_COMMAND/REDO_COMMAND, as the #6714 path does for a control outside the editor, or do nothing), and keep the active-element fallback for other input types and for Firefox's retargeting (#8738). A browser test with real key presses would cover it: type in the editor with userEvent, focus the decorator input, press Mod+Z, then assert the editor's text and that focus stays in the input. The current Mod+Z case dispatches a synthetic keydown, which cannot show what the browser's own undo does.

Optional, non-blocking:

  • Once undo is handled, re-read the changeset's Verify: line ("the editor's undo and formatting no longer run for them"); in Chromium it does not hold yet (CONTRIBUTING.md, "Changesets", asks the Verify: paragraph to say what to test).

Automated review (maintainer link)

key,
modifiers,
onMatch: (event, editor) => {
if ($isEventFromEmbeddedTextInput(event)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the undo/redo entries this hands Ctrl+Z to the browser, and in Chromium the browser's undo stack is per page: when this input has no edit of its own on top of it, Chromium undoes the user's last typing in the editor and fires beforeinput historyUndo at the editor root. onBeforeInput skips that event because the focused element is a decorator input (the active-element fallback in $isInputEventTargetingCapturedSelection, lines 1218-1223), so the #6714 branch of $handleBeforeInput (lines 1394-1416) never prevents it. In the playground: type in the editor, click into an empty poll option, press Ctrl+Z: the editor text changes with no UNDO_COMMAND, focus leaves the poll, and the next Ctrl+Z restores it. Deciding historyUndo/historyRedo by their composed target in onBeforeInput (prevent when it is the editor root, keep the fallback for Firefox's #8738 retargeting) would close it.

input.focus();
const spy = spyOn(editor, command);
const event = keyDown(input, init);
expect(event.defaultPrevented).toBe(false);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Mod+Z this synthetic keydown passes even though Chromium's real undo then rewrites the editor (see the comment on LexicalEvents.ts line 2153). A real-key case like the two at the end of the file, typing in the editor first and then pressing Mod+Z in the input, asserting the editor's text and the focused element, would cover it.

This branch was successfully deployed

2 active deployments
Preview – lexical — 822b190b Deployed Nov 17, 2026 by vercel[bot]
Preview – lexical-playground — 822b190b Deployed Nov 17, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: can't use some shortcuts in inputs inside DecoratorNodes

2 participants